/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaIngreso.java
 *
 * Created on 07-ago-2010, 20:47:39
 */

package presentacion;
import logicaClient.*;
import javax.swing.*;
import java.awt.Color;
/**
 *
 * @author Daniela
 */
public class VentanaIngreso extends javax.swing.JFrame {

    /** Creates new form VentanaIngreso */
    public VentanaIngreso() {
        initComponents();
        this.VentanaPadre3 = null;
    }

    public void setVentanaPadre2(JFrame VentanaPadre2)
    {
        this.VentanaPadre2 = VentanaPadre2;
    }

    public void VentanaIngreso(JFrame VentanaPadre3)
    {
        this.VentanaPadre3 = VentanaPadre3;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nick = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ingreso al Sistema");
        setName("VentanaIngreso"); // NOI18N

        jLabel1.setText("NickName:");

        jLabel2.setText("Clave:");

        nick.setName("nick"); // NOI18N
        nick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nickActionPerformed(evt);
            }
        });

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Aceptar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setName("jLErrorIngreso"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(nick, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jPasswordField1))))))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nick, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jLabel3.getAccessibleContext().setAccessibleName("jLErrorIngreso");

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-328)/2, (screenSize.height-197)/2, 328, 197);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
        VentanaPadre2.setVisible(true);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
//ACEPTARR!!!!! nickname clave
     //Ingreso al sistema de usuarios registrados
      datos =""; 


       datos = datos.concat("1:");
       datos = datos.concat(nick.getText());
       datos = datos.concat(":");
       datos = datos.concat(jPasswordField1.getText());

        System.out.println("Valore de dato en ingreso a usuarios registrados");
        System.out.println("=>  "+datos+"\n");
        retorno=ComunicacionJSockets.solicitarEnvioDatos(datos);
        
        if (retorno.compareTo("false") == 0 || retorno.compareTo("FALSE") == 0)
        {
            System.out.println("El nickname y la contraseña no son validos");
            jLabel3.setText("El NickName y/o Clave no son correctos");
            jLabel3.setForeground(Color.RED);
        }
        else
        {
             System.out.println("El nickname y la contraseña si son validos");

             String[] dato = retorno.split(":");
             String nombre=""; String apellido=""; 

             nombre = dato[1]; apellido=dato[2];
             System.out.println("Nombre devuelto: "+nombre);
             System.out.println("Apellido devuelto: "+apellido+"\n");
         
             ComunicacionClient.IngresoDatosUsuario(nick.getText(), jPasswordField1.getText(), nombre, apellido);
             System.out.println("\nVentana Ingreso!!!!!!!!!\n");
            System.out.println(ComunicacionClient.getLosUsuarios().toString());

            // System.out.println("IDAvatar devuelto: "+dato[3]);



            this.setVisible(false);
            VentanaJugada ventanaJugada = new VentanaJugada();
            ventanaJugada.setVisible(true);
            ventanaJugada.setVentanaPadre3(this);

        }


     /*

        if (ComunicacionClient.verificarCuenta(nick,jPasswordField1) == false)
        {
            System.out.println("El nickname y la contraseña no son validos");
          jLabel3.setText("El NickName y/o Clave no son correctos");
        }
        else
        {
            System.out.println("El nickname y la contraseña si son validos");
            this.setVisible(false);
            VentanaJugada ventanaJugada = new VentanaJugada();
            ventanaJugada.setVisible(true);
            ventanaJugada.setVentanaPadre3(this);
        }*/
    }//GEN-LAST:event_jButton2ActionPerformed

    private void nickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nickActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nickActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaIngreso().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField nick;
    // End of variables declaration//GEN-END:variables
    private JFrame VentanaPadre2;
    javax.swing.JFrame VentanaPadre3;
    private String datos = "";
    private String retorno = "";
}
